package com.thlws.rabbitmq.topic;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/***
 * 通配符模式 ,官方叫 Topic
 * BuiltinExchangeType.TOPIC
 * 本质是消费端对 bindingKey 的模糊匹配,服务端发送时为明确发送
 *
 */
public class Producer {
	
	private static final String EXCHANGE_NAME = "topic_x";

	public static void main(String[] args) throws Exception{
		
		ConnectionFactory factory = new ConnectionFactory();
		factory.setHost("116.228.212.36");
		factory.setUsername("hanley");
		factory.setPassword("hanley");
		factory.setVirtualHost("/test");
		
	    try (Connection connection = factory.newConnection();
	         Channel channel = connection.createChannel()) {

	    	//主题模式
	        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);

	        String routingKey = "cms.error.log";
	        String message = "topic主题模式测试消息";

	        channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes("UTF-8"));
	        System.out.println(" [x] Sent '" + routingKey + "':'" + message + "'");
	    }

	}

}
